function r(r, e) {
  var n = r.length,
    t = n << 2;
  if (e) {
    var a = r[n - 1];
    if (a < (t -= 4) - 3 || a > t) return null;
    t = a;
  }
  for (var o = new Uint8Array(t), i = 0; i < t; ++i)
    o[i] = r[i >>> 2] >>> ((3 & i) << 3);
  return o;
}
function e(r, e) {
  var n,
    t = r.length,
    a = t >>> 2;
  0 != (3 & t) && ++a,
    e ? ((n = new Uint32Array(a + 1))[a] = t) : (n = new Uint32Array(a));
  for (var o = 0; o < t; ++o) n[o >> 2] |= r[o] << ((3 & o) << 3);
  return n;
}
function n(r, e, n, t, a, o) {
  return (
    (((n >>> 5) ^ (e << 2)) + ((e >>> 3) ^ (n << 4))) ^
    ((r ^ e) + (o[(3 & t) ^ a] ^ n))
  );
}
function t(r) {
  if (r.length < 16) {
    var e = new Uint8Array(16);
    e.set(r), (r = e);
  }
  return r;
}
module.exports = {
  encrypt: function (a, o) {
    return null == a || 0 === a.length
      ? a
      : r(
          (function (r, e) {
            var t,
              a,
              o,
              i,
              f,
              c,
              u = r.length,
              s = u - 1;
            for (a = r[s], o = 0, c = 0 | Math.floor(6 + 52 / u); c > 0; --c) {
              for (i = ((o += 2654435769) >>> 2) & 3, f = 0; f < s; ++f)
                (t = r[f + 1]), (a = r[f] += n(o, t, a, f, i, e));
              (t = r[0]), (a = r[s] += n(o, t, a, f, i, e));
            }
            return r;
          })(e(a, !0), e(t(o), !1)),
          !1,
        );
  },
  decrypt: function (a, o) {
    return null == a || 0 === a.length
      ? a
      : r(
          (function (r, e) {
            var t,
              a,
              o,
              i,
              f,
              c = r.length,
              u = c - 1;
            for (
              t = r[0], o = 2654435769 * Math.floor(6 + 52 / c);
              0 !== o;
              o -= 2654435769
            ) {
              for (i = (o >>> 2) & 3, f = u; f > 0; --f)
                (a = r[f - 1]), (t = r[f] -= n(o, t, a, f, i, e));
              (a = r[u]), (t = r[0] -= n(o, t, a, f, i, e));
            }
            return r;
          })(e(a, !1), e(t(o), !1)),
          !0,
        );
  },
  toBytes: function (r) {
    var e = r.length;
    if (0 !== e) {
      for (var n = new Uint8Array(3 * e), t = 0, a = 0; a < e; a++) {
        var o = r.charCodeAt(a);
        if (o < 128) n[t++] = o;
        else if (o < 2048) (n[t++] = 192 | (o >> 6)), (n[t++] = 128 | (63 & o));
        else {
          if (!(o < 55296 || o > 57343)) {
            if (a + 1 < t) {
              var i = r.codeUnitAt(a + 1);
              if (o < 56320 && 56320 <= i && i <= 57343) {
                var f = 65536 + (((56320 & o) << 10) | (1023 & i));
                (n[t++] = 240 | ((f >> 18) & 63)),
                  (n[t++] = 128 | ((f >> 12) & 63)),
                  (n[t++] = 128 | ((f >> 6) & 63)),
                  (n[t++] = 128 | (63 & f)),
                  a++;
                continue;
              }
            }
            throw new Error("Malformed string");
          }
          (n[t++] = 224 | (o >> 12)),
            (n[t++] = 128 | ((o >> 6) & 63)),
            (n[t++] = 128 | (63 & o));
        }
      }
      return n.subarray(0, t);
    }
  },
  toString: function (r) {
    for (
      var e = r.length, n = new Uint16Array(e), t = 0, a = 0, o = r.length;
      t < e && a < o;
      t++
    ) {
      var i = r[a++];
      switch (i >> 4) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
          n[t] = i;
          break;
        case 12:
        case 13:
          if (!(a < o)) throw new Error("Unfinished UTF-8 octet sequence");
          n[t] = ((31 & i) << 6) | (63 & r[a++]);
          break;
        case 14:
          if (!(a + 1 < o)) throw new Error("Unfinished UTF-8 octet sequence");
          n[t] = ((15 & i) << 12) | ((63 & r[a++]) << 6) | (63 & r[a++]);
          break;
        case 15:
          if (!(a + 2 < o)) throw new Error("Unfinished UTF-8 octet sequence");
          var f =
            ((7 & i) << 18) |
            ((63 & r[a++]) << 12) |
            ((63 & r[a++]) << 6) |
            ((63 & r[a++]) - 65536);
          if (!(0 <= f && f <= 1048575))
            throw new Error(
              "Character outside valid Unicode range: 0x" + f.toString(16),
            );
          (n[t++] = ((f >> 10) & 1023) | 55296), (n[t] = (1023 & f) | 56320);
          break;
        default:
          throw new Error("Bad UTF-8 encoding 0x" + i.toString(16));
      }
    }
    return (
      t < e && (n = n.subarray(0, t)), String.fromCharCode.apply(String, n)
    );
  },
};
